using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using ZXing;
using File = System.IO.File;
using ZXing.QrCode;
public class Demo : MonoBehaviour
{
    public GameObject toHide;

    public RawImage rawImage, tex2texImage;

    //在demo里面是通过点击按钮来触发的
    public void TaskSnapshot()
    {
        StartCoroutine(TransferTexture());
    }

    //将摄像头拍摄到的图片，用提示词进行图生图，然后生成模型
    private IEnumerator TransferTexture()
    {
        //先隐藏其他UI，避免截图时拍到其他UI
        toHide.SetActive(false);
        yield return new WaitForEndOfFrame();

        //将摄像头纹理复制到一个临时texture2d贴图
        var tempTex = new Texture2D(Screen.width, Screen.height);
        var rect = new Rect(0, 0, Screen.width, Screen.height);
        tempTex.ReadPixels(rect, 0, 0);
        tempTex.Apply();

        var data = tempTex.GetPixels32();
        Result result = barcodeReader.Decode(data, tempTex.width, tempTex.height);
        //如果获取到二维码信息了，打印出来
        if (result != null)
        {
            Debug.Log("qrcode="+result.Text);//二维码识别出来的信息
            
            toHide.SetActive(true);
            rawImage.texture = tempTex;
            //这里用的gemini_2.5也就是nano_banana
            //Turbo是快速模型，20秒左右，效果差，要效果好的可用用3分钟的v3.0-20250812
            TripoAIScaner.Tex2Mesh(tempTex, "根据参考图的配色生成一个飞机", "gemini_2.5_flash_image_preview", "Turbo-v1.0-20250506",
                left => { Debug.Log($"预估时间还剩{left}秒"); }, (url,qrcode) =>
                {
                    Debug.Log($"生成的3d模型的下载地址是{url},格式为glb,对应的二维码内容是{qrcode}");
                    

                    rawImage.gameObject.SetActive(false);
                }, error => { Debug.LogError("生成失败的提示:" + error); }, tex =>
                {
                    //这里是图生图的结果，显示到UI上
                    tex2texImage.texture = tex;
                }, json =>
                {
                    //可选，这里可以修改tripo生成模型时的参数
                    //json["face_limit"] = 10000;用来限制面数，不填为不限制
                    //jsonReq["pbr"] = false;用来关闭pbr贴图，改为只生成albedo贴图
                },result.Text);
        }
        
       
    }

    private BarcodeReader barcodeReader;
    private void Start()
    {
        barcodeReader = new BarcodeReader();
        //打开摄像头
        StartCoroutine(OpenCamera());
    }

    private WebCamTexture webCamTex;
    IEnumerator OpenCamera()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            var device = devices[0];
            if (device.availableResolutions is { Length: > 0 })
            {
                Debug.Log($"找到相机{device.name}");
                webCamTex = new WebCamTexture(device.name, device.availableResolutions[0].width,
                    device.availableResolutions[0].height);
                rawImage.texture = webCamTex;
                webCamTex.Play();
            }
            else
            {
                Debug.LogError("找不到摄像头");
            }
        }
    }
}